package com.my.config;

import java.util.Map;

import com.alibaba.druid.wall.WallFilter;
import com.jfinal.config.Constants;
import com.jfinal.config.Handlers;
import com.jfinal.config.Interceptors;
import com.jfinal.config.JFinalConfig;
import com.jfinal.config.Plugins;
import com.jfinal.config.Routes;
import com.jfinal.kit.PathKit;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.activerecord.CaseInsensitiveContainerFactory;
import com.jfinal.plugin.activerecord.dialect.MysqlDialect;
import com.jfinal.plugin.activerecord.dialect.OracleDialect;
import com.jfinal.plugin.activerecord.dialect.SqlServerDialect;
import com.jfinal.plugin.activerecord.generator.DataDictionaryGenerator;
import com.jfinal.plugin.activerecord.generator.MetaBuilder;
import com.jfinal.plugin.druid.DruidPlugin;
import com.my.app.model.system.Sys_User;
import com.my.common.DictKeys;
import com.my.core.beetl.MyBeetlRenderFactory;
import com.my.core.kit.CFKit;
import com.my.core.kit.ConfigKit;
import com.my.core.plugin.ConfigPlugin;
import com.my.core.plugin.RoutesPlugin;
import com.my.core.plugin.TablelPlugin;

public class ProjectConfig extends JFinalConfig{

	public void configConstant(Constants me) {
		new ConfigPlugin(".*.txt").reload(false).start();
		
		me.setDevMode(ConfigKit.getBoolean(DictKeys.conf_devMode, false));
		me.setMainRenderFactory(new MyBeetlRenderFactory());
		
		me.setError404View("/error/404.html");
		me.setError401View("/error/401.html");
		me.setError403View("/error/403.html");
		me.setError500View("/error/500.html");
	}

	/**配置路由*/
	public void configRoute(Routes me) {
		//路由扫描注册
		new RoutesPlugin(me).start();
	}

	
	/** 
	 * 配置插件
	 */
	public void configPlugin(Plugins me) {
		String[] dbConfigs = ConfigKit.getStr(DictKeys.db_configs).split(",");
		if (dbConfigs.length == 0) {
            throw new RuntimeException("Unable to find any database config. ");
        }
		
		//可配置多数据源
		Map<String, ActiveRecordPlugin> arpMap = CFKit.newMap();
		int initialSize = ConfigKit.getInt(DictKeys.db_initialSize, 50);
		int minIdle = ConfigKit.getInt(DictKeys.db_minIdle, 50);
		int maxActive = ConfigKit.getInt(DictKeys.db_maxActive, 100);
		
		for (String config : dbConfigs) {
			String dbType = ConfigKit.getStr(getKey(config, DictKeys.db_type));
			String jdbcUrl = ConfigKit.getStr(getKey(config, DictKeys.db_jdbcUrl));
			String user = ConfigKit.getStr(getKey(config, DictKeys.db_user));
			String password = ConfigKit.getStr(getKey(config, DictKeys.db_password));
			String driverClass = ConfigKit.getStr(getKey(config, DictKeys.db_driverClass), null);
			DruidPlugin druidPlugin = null;
			if (driverClass != null) {
				druidPlugin = new DruidPlugin(jdbcUrl, user, password);
			} else {
				druidPlugin = new DruidPlugin(jdbcUrl, user, password, driverClass);
			}
			druidPlugin.set(initialSize, minIdle, maxActive);
			WallFilter wallFilter = new WallFilter();
			ActiveRecordPlugin arp = new ActiveRecordPlugin(config, druidPlugin);
			
			if (dbType.equalsIgnoreCase("mysql")) {
				wallFilter.setDbType("mysql");
				arp.setDialect(new MysqlDialect());
			} else if (dbType.equalsIgnoreCase("oracle")) {
				wallFilter.setDbType("oracle");
				arp.setDialect(new OracleDialect());
				arp.setContainerFactory(new  CaseInsensitiveContainerFactory());
			} else if (dbType.equalsIgnoreCase("sqlserver")) {
				wallFilter.setDbType("sqlserver");
				arp.setDialect(new SqlServerDialect());
			}
			
			druidPlugin.addFilter(wallFilter);
			me.add(druidPlugin);
			me.add(arp);
			arpMap.put(config, arp);
		}
		me.add(new TablelPlugin(arpMap));
	}

	public void configInterceptor(Interceptors me) {
		
	}

	public void configHandler(Handlers me) {
		
	}
	
	
	@Override
	public void afterJFinalStart() {
		System.err.println(Sys_User.dao.find("select * from sys_user"));
	}
	
	
	private String getKey(String config, String key) {
		return String.format("%s.%s", config, key);
	}
}
